package com.example.thread;

public class TestBuyTickes {


    public static void main(String[] args) {
        ButTickes a = new ButTickes();
        new Thread(a,"老板").start();
        new Thread(a,"小明").start();
    }


}


class ButTickes implements Runnable
{

    private int tickes = 10;
    Boolean flag = true;

    @Override
    public void run() {
       while (flag)
       {
           try {
               buy();
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }

    }

    private synchronized void buy() throws InterruptedException {
        if(tickes<=0)
        {
            flag = false;
            return;
        }
        Thread.sleep(100);
        System.out.println(Thread.currentThread().getName()+tickes--);
    }
}
